angular.module("application")
.controller('userCtrl', ['$scope', '$http', '$location', '$timeout', 'BASE_URL', 
	function ($scope, $http, $location, $timeout, BASE_URL) 
{	
	$http.post(BASE_URL + 'user/check_session').then(function(promise) {
		$scope.userName = promise.data;
	})

	getSesson = function() {
		return $scope.$parent.isLogin;
	}
	$scope.$watch(getSesson, function(val) {
		if(!val) $location.path('/');
	})

	$timeout(function() {
		$scope.$apply(function() {
			$scope.$parent.loadingView = false;
		})
	},500);
	
}])

// =========================================================================================================================
// =========================================================================================================================
.controller('userActCtrl', function ($scope, $location, $routeParams, $http, BASE_URL) 
{	
	$scope.action = $routeParams.action;
	$scope.userName = "";
	if($scope.action != 'comment' && $scope.action != 'favourite' && $scope.action != 'profile') {
		$location.path('/');
	}
	$http.post(BASE_URL + 'user/check_session')
	.then(function(promise) 
	{
		$scope.userName = promise.data;
	});

	switch($scope.action) {
		case 'comment': 
			$http.post(BASE_URL + 'application_api/comment/getUserComment/')
			.then(function(promise) {
				console.log(promise.data);
				$scope.commentList = promise.data;

				setTimeout(function() {
					$scope.$apply(function() {
						$scope.$parent.loadingView = false;
					})
				}, 500);
			}); 
			break;
		case 'favourite': {
			$http.post(BASE_URL + 'user/getBookmark')
			.then(function(prm) {
				var list = prm.data;
				return $http({
					url: BASE_URL + 'application_api/lesson/getArrLesson',
					method: "POST",
					data: $.param({'lsList': list}),
					headers: {'Content-Type': 'application/x-www-form-urlencoded'}
				})
			}).then(function(prm) {
				$scope.favList = prm.data;

				setTimeout(function() {
					$scope.$apply(function() {
						$scope.$parent.loadingView = false;
					})
				}, 500);
			});
				
			break;
		}
	}

	getSesson = function() {
		return $scope.$parent.isLogin;
	}
	$scope.$watch(getSesson, function(val) {
		if(!val) $location.path('/');
	});


	$scope.delCmt = function(id) {
		$http.post(BASE_URL + 'application_api/comment/getDelete/' + id)
		.then(function(promise) {
			getAlert('Đã xóa');
		});
	}

	$scope.delFav = function(index) {
		var id = $scope.favList[index]['_id'];
		$http.post(BASE_URL + 'user/removeBookmark/' + id)
		.then(function(promise) {
			getAlert('Đã xóa');
			$scope.favList.splice(index, 1);
		});
	}

})
